<?php
require_once("Application.php");
require_once(SITE_ROOT.'/includes/tools/id.php');
require_once(SITE_ROOT.'/includes/Security.php');
require_once(SITE_ROOT.'/includes/tools/mt_utils.php');
require_once(SITE_ROOT."/includes/tools/mt_thumb.php");
//require_once(SITE_ROOT.'/includes/User.php');

class SignApplication extends Application {
    private $points_signup_basic = 20;      // 注册初始化为20积分
	
	public function __construct($id, $name, $default_privacy,$day_max_points) {
        parent::__construct($id, $name, $default_privacy,$day_max_points);
    }
    // returns the URL of the small icon representing this application
	public function getSmallIcon() {
		return "";
	}

	// returns the URL of the large icon representing this application
	public function getLargeIcon() {
		return "/notfound.html";
	}

	// returns the URL of the main page for this application
	public function getMainPageURL() {
		return "";
	}
	public function postLogin($username,$pwd){
		global $mysqli_read,$mysqli_write;
		$username = mysql_escape_string($username);
		$pwd = mysql_escape_string($pwd);
		$sql = "SELECT * FROM mt_users where username='$username' and userpass='$pwd' limit 1";
		$result = $mysqli_read->query($sql);
		if($result->num_rows == 1){
			$row = $result->fetch_array(MYSQLI_ASSOC);
			$_SESSION['G_USERID']=$row["id"];
			$_SESSION['G_USERNAME']=$row["username"];
			$_SESSION['G_NICKNAME']=$row["nickname"];
			$_SESSION['G_USERSEX']=$row["sex"];
			$sessionid = session_id();
			$userIp = get_real_ip();
			$this->updateAfterLogin($row["id"],$sessionid,$userIp,0);
			$result->free();
			return 0;
		}else{
			return 1;	//no such user
		}
	}
    /**
     * @abstract 注册用户
     * @param 注册基本信息
     * */
    public function signUp($username,$password,$nickname,$sex){
    	global $mysqli_read,$mysqli_write;
    	$username = mysql_escape_string($username);
    	$password = mysql_escape_string($password);
    	$nickname = mysql_escape_string($nickname);
    	$sex = mysql_escape_string($sex)?'0':'1';
    	$error = 0;
    	$mysqli_write->autocommit(false);
    	$id = get_unique_id();
    	$ip = get_real_ip();
    	$mysqli_write->query("INSERT INTO mt_users(id,username,userpass,nickname,regdate,sex) VALUES ('$id','$username','$password','$nickname',now(),'$sex')");
    	$error += $mysqli_write->errno;
    	//创建用户文件夹
    	if(!file_exists(SITE_ROOT."/static/user/".$id))
    		mkdir(SITE_ROOT."/static/user/".$id);
    	if(!file_exists(SITE_ROOT."/static/user/".$id."/face"))
    		mkdir(SITE_ROOT."/static/user/".$id."/face");
    	if(!file_exists(SITE_ROOT."/static/user/".$id."/css"))
    		mkdir(SITE_ROOT."/static/user/".$id."/css");
    	if(!file_exists(SITE_ROOT."/static/user/".$id."/images"))
    		mkdir(SITE_ROOT."/static/user/".$id."/images");
    	if(!file_exists(SITE_ROOT."/static/user/".$id."/xml"))
    		mkdir(SITE_ROOT."/static/user/".$id."/xml");
    	//设置默认皮肤与css:10
    	copy(SITE_ROOT."/images/template/10/css/10.css",SITE_ROOT."/static/user/".$id."/css/".$id.".css");
    	//为用户设置一个自定义的flash版的相册xml列表
    	copy(SITE_ROOT."/static/xml/album.xml",SITE_ROOT."/static/user/".$id."/xml/album.xml");
    	//为用户设置一个自定义的音乐播放器的xml列表
    	
    	//为用户设置一个自定义的空间模块基本设置的xml列表
    	copy(SITE_ROOT."/static/xml/space.xml",SITE_ROOT."/static/user/".$id."/xml/space.xml");
    	//为用户添加系统默认的一个编辑为好友
    	
    	//加入用户到官方群组
    	
    	//为用户建立第一篇日志
    	
    	//为用户建立一个自定义相册
    	$mysqli_write->query("INSERT INTO mt_album(id,albumid,user_id,albumname,albumdesc,ifchecked) VALUES ('".get_unique_id()."','1','$id','我的第一个相册','我的第一个相册',1)");
    	$error += $mysqli_write->errno;    	
    	if($error == 0){
    		$mysqli_write->commit();
    		//记录session
    		session_start();
			$sessionid = session_id();
			$this->updateAfterLogin($id,$sessionid,$ip);
			$_SESSION['G_USERID']=$id;
			$_SESSION['G_USERSEX']=$sex;
			$_SESSION['G_USERNAME']=$username;
			$_SESSION['G_NICKNAME']=$nickname;
			$_SESSION['G_REFRESHTIME']="0";
			$_SESSION['G_FIRSTCOMEHERE'] = '1';
    		$mysqli_write->autocommit(true);
			return 0; //成功
    	} else {
    		$mysqli_write->rollback();
    		$mysqli_write->autocommit(true);
    		return 1; ///mysql 错误
    	}    	
    }    
    /**
     * 在用户登录成功后，更新一些该用户资料
     *
     * @param $userId 登录者的id
     * @return  true:成功  false：失败
     */
    public function updateAfterLogin( $userId,$sessionId,$loginIp,$extended=0 ) {
    	global $mysqli_write,$gSession;
    	$userId = mysql_escape_string($userId);
    	$sessionId = mysql_escape_string($sessionId);
    	$loginIp = mysql_escape_string($loginIp);
    	$mysqli_write->query("REPLACE mt_user_sessionid SET user_id='$userId',sessionid='$sessionId'");
    	$mysqli_write->query("UPDATE mt_users SET lastlogtime=NOW(),lastseen=NOW(),lastlogip='$loginIp' WHERE id='$userId'");
    	if($gSession)
    		$gSession->setUser($sessionId,$userId,$extended);
    }
    
    public function maidou_1($userId,$birth,$edu,$career,$single,$smoke,$drink,$pet,$blood,$intro){
    	global $mysqli_write;
    	$userId = mysql_escape_string($userId);
    	$birth = mysql_escape_string($birth);
    	$edu = mysql_escape_string($edu);
    	$career = mysql_escape_string($career);
    	$single = mysql_escape_string($single);
    	$smoke = mysql_escape_string($smoke);
    	$drink = mysql_escape_string($drink);
    	$pet = mysql_escape_string($pet);
    	$blood = mysql_escape_string($blood);
    	$intro = mysql_escape_string($intro);    	
    	$birth = explode("-",$birth);
    	$mysqli_write->query("UPDATE mt_users set birth_year='$birth[0]',birth_month='$birth[1]',birth_day='$birth[2]',edu='$edu',career='$career',single='$single',smoke='$smoke',drink='$drink',pet='$pet',blood='$blood',introduce='$intro' WHERE id='$userId'");
    	if($mysqli_write->errno == 0)
    		return 0;
    	else
    		return 1;    	
    }
    
    public function setuploadUserFace($userid,$file){
    	global $mysqli_read,$mysqli_write;
    	$userid = $mysqli_read->real_escape_string($userid);    	
    	$uploaddir=SITE_ROOT."/static/user/{$userid}/face/";//上传的图片路径
    	if(!file_exists($uploaddir))
    		return -1;			//错误的文件
	    $fileName=$file;//文件名
	    $fileNameTemp=time();//当前时间用做图片的文件名
	    $fileExtenName=strrchr($fileName,".");//取出文件的扩展名
	    $fileName_new=$fileNameTemp.$fileExtenName;//图片文件名
	    rename($uploaddir.$file,$uploaddir.$fileName_new);
	    $fileName = $fileName_new;
	    $filePath=$uploaddir.$fileName;//文件保存全路径	    
		$mysqli_write->query("UPDATE mt_user_face SET src='$fileName' WHERE user_id='$userid' ang ismain=1" );		
		$t=new ThumbHandler();//图片处理类
		//下面的操作生成一系列大小规格的图片							
		$t->setSrcImg($uploaddir.$fileNameTemp.$fileExtenName);
		$t->setCutType(1);
		$t->setDstImg($uploaddir.$fileNameTemp."_xl".$fileExtenName);
		$t->createImg(128,128); 
		$t->setSrcImg($uploaddir.$fileNameTemp.$fileExtenName);
		$t->setCutType(1);
		$t->setDstImg($uploaddir.$fileNameTemp."_l".$fileExtenName);
		$t->createImg(48,48);    
		$t->setSrcImg($uploaddir.$fileNameTemp.$fileExtenName);
		$t->setCutType(1);   
		$t->setDstImg($uploaddir.$fileNameTemp."_m".$fileExtenName);
		$t->createImg(32,32);   
		$t->setSrcImg($uploaddir.$fileNameTemp.$fileExtenName);
		$t->setCutType(1);    
		$t->setDstImg($uploaddir.$fileNameTemp."_s".$fileExtenName);
		$t->createImg(16,16);
		$picName=$uploaddir.$fileNameTemp."_l".$fileExtenName;				
		return $fileNameTemp."_m".$fileExtenName; 
		//将上传成功的图片动态添加到页面中去
    }
    /**
     * 获得用户详细资料
     *
     * @param unknown_type $userId
     */
    public function getUserDetail($userId){
    	
    }
    public function getUserSimpleDetail($userId){
    	global $mysqli_read;
    	$userId = mysql_escape_string($userId);
    	$res = $mysqli_read->query("SELECT u.*,p.src FROM mt_users u LEFT JOIN mt_user_face p ON(p.user_id=u.id AND p.ismain=1) WHERE u.id='$userId'");
    	$row = $res->fetch_array(MYSQLI_ASSOC);
    	return $row;
    }
    
}
?>
